import uma from 'umtrack-wx'
import { Analytic_Type } from '@CONSTANTS/analytic'
import { painManagementRecord } from '@UTILS/track/trackUtils'
import { isInternalAccount } from './internalAccount'
// import Taro from '@tarojs/taro'
// import './mtj-wx-sdk'

// let mtjInstance;
// const getMtjInstance = () => {
//     if (!mtjInstance) {
//         // eslint-disable-next-line no-undef
//         mtjInstance = getApp().mtj;
//     }
//     return mtjInstance
// }

export const trackEvent = async (trackId, obj, sceneData) => {
  try {
    if (await isInternalAccount()) {
      return
    }
    if (sceneData) {
      painManagementRecord(trackId, obj, sceneData)
    }
    if (Analytic_Type[trackId]?.isOnlyRecord) return
    const data = {}
    Object.assign(data, obj, sceneData)
    uma.trackEvent(trackId, data)
  } catch (err) {
    console.log('TrackerEvent Error:', err)
  }
}

const setUserId = async userId => {
  try {
    console.log('SetUserId', userId)
    // getMtjInstance().setUserId(userId)
  } catch (err) {
    console.log('TrackerEvent Error:', err)
  }
}

const init = appId => {
  uma.init({
    appKey: appId, // '5f96f3351c520d3073990542'
    useOpenid: false,
    autoGetOpenid: false,
    debug: false,
    uploadUserInfo: true
  })
}

export default { trackEvent, setUserId, init }
